package Views;

import Model.Grid;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Observable;
/**
 * 
 * @author KOK Leng-Steven & RIEU Simon
 */
public class RectangleView extends GridView
{

    public RectangleView(Grid grid)
    {
        super(grid);
        strategies = new Rectangle[this.size][this.size];
        for (int i = 0; i < this.size; i++)
        {
            for (int j = 0; j < this.size; j++)
            {
                strategies[i][j] = new Rectangle(20, 20, i * 20,  j * 20);
            }
        }
        this.colorRectangleView(grid);
    }

    @Override
    public void paintComponent(Graphics g)
    {
        for (int i = 0; i < this.size; i++)
        {
            for (int j = 0; j < this.size; j++)
            {
                strategies[i][j].draw(g);
            }
        }
    }

    @Override
    public void update(Observable o, Object o1)
    {
        if (o instanceof Grid)
        {
            Grid grid = (Grid) o;
            this.colorRectangleView(grid);

        }
    }

    private void colorRectangleView(Grid grid)
    {

        Rectangle r;

        //everybody have a white fill by default
        for (int i = 0; i < this.size; i++)
        {
            for (int j = 0; j < this.size; j++)
            {
                r = (Rectangle) strategies[i][j];
                r.setColor(Color.WHITE);

            }
        }
        //we fill in red the rectangle where the avatar currenty is
        r = (Rectangle) strategies[grid.getAvatar().getI()][grid.getAvatar().getJ()];
        r.setColor(Color.RED);

        //we fill the circles where there are byStanders
        for (int i = 0; i < grid.getByStanders().size(); i++)
        {
            r = (Rectangle) strategies[grid.getByStanders().get(i).getI()][grid.getByStanders().get(i).getJ()];
            r.setColor(Color.GRAY);
            //If the avatar is seeing someone we change the color GRAY to BLUE
            if (grid.getByStanders().get(i).isAvatarSeeingByStander())
            {
                r.setColor(Color.BLUE);
            }
        }
        this.repaint();
    }
}
